package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.AddressDTO;
import cn.ulc.socket.weapp.model.dto.AddressListDTO;
import cn.ulc.socket.weapp.model.dto.CityListDTO;
import cn.ulc.socket.weapp.model.param.AddressSaveParam;
import cn.ulc.socket.weapp.service.AddressServ;
import cn.ulc.store.dao.CitysDao;
import cn.ulc.store.dao.user.MemberDao;
import cn.ulc.store.dao.user.ReceiveAddressDao;
import cn.ulc.store.entity.Citys;
import cn.ulc.store.entity.user.Member;
import cn.ulc.store.entity.user.ReceiveAddress;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AddressServImpl extends BaseService implements AddressServ {
    ReceiveAddressDao addressDao;
    MemberDao memberDao;
    CitysDao citysDao;

    @Override
    public CityListDTO findCityList() {
        List<Citys> areaList = citysDao.findList();
        CityListDTO dto = new CityListDTO();
        if(areaList.size()>0){
            Map<Integer,List<Citys>> cList = areaList.stream().collect(Collectors.groupingBy(Citys::getLevel));
            List<Citys> provinceList = cList.get(2);
            List<Citys> cityList = cList.get(3);
            List<Citys> countyList = cList.get(4);
            Map<String,String> provinceMap = provinceList.stream().collect(Collectors.toMap(Citys::getCode,Citys::getName));
            Map<String,String> cityMap = cityList.stream().collect(Collectors.toMap(Citys::getCode,Citys::getName));
            Map<String,String> countyMap = countyList.stream().collect(Collectors.toMap(Citys::getCode,Citys::getName));
            dto.setProvinceMap(provinceMap);
            dto.setCityMap(cityMap);
            dto.setCountyMap(countyMap);
        }
        return dto;
    }

    @Override
    public AddressListDTO findList() {
        WeixinUser loginUser = getCurrentUser();
        List<ReceiveAddress> addressList = addressDao.findList(loginUser.getId());
        return new AddressListDTO(addressList);
    }

    @Override
    public Boolean save(AddressSaveParam param) {
        WeixinUser loginUser = getCurrentUser();
        Member member = memberDao.findByWxUserId(loginUser.getId(),AvailableStatus.AVAILABLE);
        ReceiveAddress address = new ReceiveAddress();
        setBaseInfo(address);
        address.setIsDefault(param.getIsDefault());
        address.setName(param.getName());
        address.setPhone(param.getPhone());
        address.setArea(param.getArea());
        address.setProvince(param.getProvince());
        address.setCity(param.getCity());
        address.setCounty(param.getCounty());
        address.setAddress(param.getAddress());
        address.setWxUserId(loginUser.getId());
        if(member!=null)address.setMemberId(member.getId());
        if(param.getIsDefault()==true){
            ReceiveAddress check = addressDao.findByCondition(loginUser.getId(),true);
            if(check!=null) {
                setBaseInfo(check);
                check.setIsDefault(false);
                addressDao.save(check);
            }
        }
        addressDao.save(address);
        return true;
    }

    @Override
    @Transactional
    public Boolean update(Long id, AddressSaveParam param) {
        WeixinUser loginUser = getCurrentUser();
        ReceiveAddress address = addressDao.findOne(id);
        if(address==null) throw new PlatformException("获取");
        setBaseInfo(address);
        address.setName(param.getName());
        address.setPhone(param.getPhone());
        address.setArea(param.getArea());
        address.setProvince(param.getProvince());
        address.setCity(param.getCity());
        address.setCounty(param.getCounty());
        address.setAddress(param.getAddress());
        if(param.getIsDefault()==true && address.getIsDefault()==false){
            ReceiveAddress check = addressDao.findByCondition(loginUser.getId(),true);
            if(check!=null) {
                setBaseInfo(check);
                check.setIsDefault(false);
                addressDao.save(check);
            }
        }
        address.setIsDefault(param.getIsDefault());
        addressDao.save(address);
        return true;
    }

    @Override
    public AddressDTO findDetail(Long id) {
        ReceiveAddress address = addressDao.findOne(id);
        return new AddressDTO(address);
    }

    @Override
    public AddressDTO findDefaultAddress() {
        WeixinUser loginUser = getCurrentUser();
        ReceiveAddress address = addressDao.findByCondition(loginUser.getId(),true);
        if(address==null){
            List<ReceiveAddress> addressList = addressDao.findList(loginUser.getId());
            if(addressList.size()>0) address = addressList.get(0);
        }
        return new AddressDTO(address);
    }

    @Override
    public Boolean deleted(Long id) {
        ReceiveAddress address = addressDao.findOne(id);
        if(address==null) throw new PlatformException("获取收货地址信息失败!");
        setBaseInfo(address);
        address.setDeleted(DeletedStatus.DELETED);
        addressDao.save(address);
        return true;
    }
}
